package com.cino.demos.chain;

import java.util.ArrayList;
import java.util.List;

public class FilterChain implements Filter{

    //记录当前执行的步骤
    int cursor = 0;

    //filter的链
    List<Filter> filterChain = new ArrayList<>();

    //filter的链
    My target;

    public My getTarget() {
        return target;
    }

    public void setTarget(My target) {
        this.target = target;
    }

    void addFilter(Filter filter){
        filterChain.add(filter);
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        //第一次执行第一个filter，依次往下

        //游标小于总数量filter一直往下获取执行
        if (cursor< filterChain.size()) {
            Filter filter = filterChain.get(cursor);

            cursor++;
            System.out.println("当前request:"+request);
            System.out.println("当前response:"+response);
            //执行filter
            filter.doFilter(request, response, chain);
        }else {
            //filter完了，该执行目标方法了
            target.hello();
        }

    }
}
